package com.leoman.spinningmachine.tab3;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.leoman.helper.util.AppManager;
import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.login.LoginActivity;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018-05-08.
 */
public class UpPhoneActivity extends BaseActivity{

    @BindView(R.id.et_phone)
    EditText et_phone;

    @BindView(R.id.tv_yzcode)
    TextView tv_yzcode;

    @BindView(R.id.et_yzcode)
    EditText et_yzcode;

    @BindView(R.id.btn_save)
    Button btn_save;
    private String newPhoneNum;
    private String yzCode;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_up_phone_num;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initTitle("手机号码");
//        et_phone.setText(SharedPreferencesUtils.getInstance().getString(Constant.PHONE));
        btn_save.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.btn_save,R.id.tv_yzcode})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_save:
                upPhoneNum();
                break;
            case R.id.tv_yzcode:
                getCode();
                break;
        }
    }

    private void upPhoneNum() {
        newPhoneNum = et_phone.getText().toString().trim();
        yzCode = et_yzcode.getText().toString().trim();
        if(TextUtils.isEmpty(newPhoneNum)){
            DialogUtils.getInstance().showResultDialog(this,"请输入新手机号");
            return ;
        }
        if(!newPhoneNum.matches(Constant.ISPHONENUM)){
            DialogUtils.getInstance().showResultDialog(this,"手机号格式不正确");
            return;
        }
        if(newPhoneNum.equals(SharedPreferencesUtils.getInstance().getString(Constant.PHONE))){
            return ;
        }
        DialogUtils.getInstance().show(this);
        WebServiceApi.getInstance().upPhoneNum(SharedPreferencesUtils.getInstance().getString("token"),newPhoneNum,yzCode,this,2);
    }

    private void getCode() {
        newPhoneNum = et_phone.getText().toString().trim();
        if(TextUtils.isEmpty(newPhoneNum)){
            DialogUtils.getInstance().showResultDialog(this,"请输入新手机号");
            return ;
        }
        if(!newPhoneNum.matches(Constant.ISPHONENUM)){
            DialogUtils.getInstance().showResultDialog(this,"手机号格式不正确");
            return;
        }
        if(newPhoneNum.equals(SharedPreferencesUtils.getInstance().getString(Constant.PHONE))){
            DialogUtils.getInstance().showResultDialog(this,"新手机号与旧手机号一致");
            return ;
        }
        sentVetify(tv_yzcode);
        WebServiceApi.getInstance().sendcodeRequest(newPhoneNum,"changePhone",this,1);
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        super.OnSuccessData(apiResponse,tag);
        if(1 == tag){
            et_yzcode.setText(""+apiResponse.data.code);
        }else if(2 == tag){
            SharedPreferencesUtils.getInstance().clear();
            AppManager.getInstance().AppExit(this);
            gotoOtherActivity(LoginActivity.class);
            ToastUtil.showToast(this,apiResponse.message,Toast.LENGTH_SHORT);
            finish();
        }
    }

    @Override
    public void OnErrorData(String code, Integer tag) {
        super.OnErrorData(code,tag);
    }

    @Override
    public void OnFailureData(String error, Integer tag) {
        super.OnFailureData(error,tag);
    }
}
